package com.example.treasure_back.controller;


import com.alibaba.fastjson.JSONObject;
import com.example.treasure_back.entity.MsgEntity;
import com.example.treasure_back.mapper.EssayMapper;
import com.example.treasure_back.model.EssayModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/treasure")
public class EditorController {

    @Autowired
    EssayMapper essayMapper;

    @RequestMapping("/editor")
    public String toEdit(){
        return "editor";
    }

    @RequestMapping("/upload")
    @ResponseBody
    public MsgEntity upload(@RequestBody JSONObject jsonObject){
        EssayModel essayModel = new EssayModel();
        essayModel.setTitle(jsonObject.getString("title"));
        essayModel.setAuthor(jsonObject.getString("author"));
        essayModel.setContent(jsonObject.getString("content"));
        essayModel.setImageUrl(jsonObject.getString("imageUrl"));
        essayModel.setLikeNum(0);
        essayModel.setDislikeNum(0);
        essayMapper.insert(essayModel);
        return new MsgEntity("SUCCESS","200","OK");
    }

    @RequestMapping("/map-index")
    public String toMap(Model model){
        model.addAttribute("trash","https://4myblog.oss-cn-beijing.aliyuncs.com/treasure/trash.csv");
        return "map";
    }

}
